package com.siteseat.crudeq.kernel;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class MessageHistory {

  private List<SendInfo> sendInfos;

  private int            totalSendSize;

  public static class SendInfo {
    public int       offset;

    public int       size;

    public ChunkInfo info;
  }

  private Integer queueId;

  /**
   * @param chunks
   */
  public MessageHistory( Integer queueId, List<ChunkInfo> chunks, int offset ) {
    this.queueId = queueId;

    int n = chunks.size();
    this.sendInfos = new ArrayList<SendInfo>( n );

    long total = 4 + 4; // queue id + chunk count

    for( int i = 0; i < n; i++ ) {
      ChunkInfo ci = chunks.get( i );
      SendInfo s = new SendInfo();
      s.info = ci;

      if( i == 0 ) {
        s.offset = offset;
      } else {
        s.offset = 0;
      }

      long chunkSize = ci.getSize() - s.offset;
      s.size = (int)chunkSize;

      chunkSize += 8 + 4 + 4;// chunk id + offset + chunk size
      if( total + chunkSize >= Integer.MAX_VALUE ) {
        break;
      }
      this.sendInfos.add( s );

      total += chunkSize;// offset + chunkSize
    }

    this.totalSendSize = (int)total;
  }

  /**
   * Gets the queueId
   *
   * @return the queueId
   */
  public Integer getQueueId() {
    return queueId;
  }

  /**
   * Gets the totalSendSize
   * 
   * @return the totalSendSize
   */
  public int getTotalSendSize() {
    return totalSendSize;
  }

  /**
   * @return
   */
  public List<SendInfo> getSendInfos() {
    return this.sendInfos;
  }

}
